package club.banyuan.hrms.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(name = "ServletTest", urlPatterns = "/st1")
public class ServletTest1 extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置Content-Type
        response.setContentType("text/plain;charset=UTF-8");

        String method = request.getMethod();
        String queryString = request.getQueryString();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        response.getWriter().println("method: " + method);
        response.getWriter().println("queryString: " + queryString);
        response.getWriter().println("username: " + username);
        response.getWriter().println("password: " + password);

        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String url = request.getRequestURL().toString();
        String uri = request.getRequestURI();

        response.getWriter().println("contextPath: " + contextPath);
        response.getWriter().println("servletPath: " + servletPath);
        response.getWriter().println("url: " + url);
        response.getWriter().println("uri: " + uri);

        // 枚举查询字符串
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = request.getParameter(name);
            response.getWriter().println(name + ": " + value);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
